var path = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var OpenBrowserPlugin = require('open-browser-webpack-plugin');
var autoprefixer = require("autoprefixer");
var HtmlWebpackInlineSourcePlugin = require('html-webpack-inline-source-plugin'); //内联的引入script

module.exports = {
    devtool: 'eval-source-map',
    entry: {
        index: ['./src/public-resource/logic/index/index.js'],
        main: ['./src/public-resource/logic/index/main.js'],
        show: ['./src/public-resource/logic/index/show.js']
    },
    output: {
        filename: 'js/[name].js',
        path: __dirname + '/build',
        publicPath: '/'
    },
    module: {
        rules: [
            { test: /\.json$/, use: 'json-loader' },
            {
                test: /\.(css|scss)$/,
                exclude: /(node_modules|bower_components)/,
                use: ExtractTextPlugin.extract({
                        fallback: "style-loader",
                        use: ['css-loader','sass-loader']
                    })
                 
            },
            {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['es2015']
                    }
                }
            },
            {
                test: /\.html$/,
                exclude: /(node_modules|bower_components)/,
                use: [{
                    loader: 'html-withimg-loader',
                    options: {
                        minimize: false
                    }
                }]
            },
            { test: /\.hbs$/,exclude: /(node_modules|bower_components)/, use: "handlebars-loader" },
            {
                test: /\.(png|jpg|gif|svg)$/,
                exclude: /(node_modules|bower_components)/,
                use: [{
                        loader: "file-loader",
                        query: {
                             useRelativePath: process.env.NODE_ENV === "production",
                            name:'./assets/images/[name]-[hash:6].[ext]',
                        }
                    },
                    // {
                    //     loader: "url-loader",
                    //     options: {
                    //         name: './assets/images/[name]-[hash:6].[ext]',
                    //         // limit: 8024
                    //     }
                    // },
                    {
                        loader: "image-webpack-loader",
                        query: {
                            progressive: true,
                            // optimizationLevel: 7,
                            // interlaced: false,
                            pngquant: {
                                quality: '65-90',
                                speed: 4
                            }
                        }
                    }
                ]
            },
           

        ]

    },

    resolve: {
        extensions: ['.json', '.js', '.jsx', '.css', '.scss', '.ejs', '.html', '.hbs'],
      
    },
    devServer: {
        contentBase: "./build", //本地服务器所加载的页面所在的目录
        host: 'localhost', //端口
        port: 8089, //端口
        hot: true, 
        historyApiFallback: false, //不跳转
        inline: true //实时刷新
    },
    plugins: [
        new webpack.LoaderOptionsPlugin({
            debug: false,
            options: {
                postcss: [
                    autoprefixer()
                ],
            },
        }),
        new ExtractTextPlugin('./assets/css/style.[hash:6].css'),
        // new webpack.optimize.UglifyJsPlugin(), //混淆JS
        new HtmlWebpackPlugin({ //
            title: "Webpack Demo", //页面标题
            filename: './index.html', //生成的HTML文件名
            template: 'index.ejs', //模板文件
            //inject: false, //入口文件引入的位置“head”和“body”
            date: new Date(),
            //chunks: ['index'], //只引入自己需要的入口文件
            excludeChunks: ['show'], //排除的入口文件，剩下的都引入
            //inlineSource: '.(js|css)$', //内联的引入script
            minify: {
                removeComments: true, //删除注释
                removeEmptyAttributes: true, //删除空格
                minifyCSS: true, //压缩css
                minifyJS: true, //压缩js
            }
        }),
        new HtmlWebpackPlugin({
            title: "Webpack Demo ShowPage",
            filename: './pages/show.html',
            template: 'index.ejs',
            //inject: false,
            date: new Date(),
            chunks: ['main', 'show'] //只引入自己需要的入口文件
        }),
        new HtmlWebpackInlineSourcePlugin(), //内联的引入script
        new OpenBrowserPlugin({
            url: "http://localhost:8089"

        })
    ]


};